package com.gshocklab.bulkposter.view.helper;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gshocklab.bulkposter.R;
import com.gshocklab.bulkposter.data.AccountTypesProvider;
import com.gshocklab.bulkposter.data.dto.SocialAccount;
import com.gshocklab.bulkposter.view.adapter.AccountsListAdapter;
import com.gshocklab.bulkposter.view.adapter.AccountsTypesListAdapter;

/**
 * Application dialogs
 * @author vitaly gashock
 */
public final class ApplicationDialogs {
	/**
	 * Setup account dialog callback interface
	 */
	public interface ISetupAccountDialogListener {
		public void dataGathered(SocialAccount socialAccount);
	}
	
	public interface ISelectAccountDialogListener {
		public void accountSelected(final SocialAccount account);
	}
	
	/**
	 * Confirm dialog callback interface
	 */
	public interface IConfirmDialogListener {
		public void onYes();
		public void onNo();
	}
	
	
	/**
	 * Display confirm dialog
	 * @param context current context for dialog
	 * @param title dialog header
	 * @param message confirmation message text
	 * @param listener dialog callback which will be invoked when user taps on dialog buttons
	 */
	public static void showConfirmDialog(Activity context, String title, String message, final IConfirmDialogListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		builder.setTitle(title);
		builder.setIcon(R.drawable.question_icon);
		builder.setMessage(message);
		
		builder.setPositiveButton("Yes", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int buttonId) {
				listener.onYes();
			}
		});
		
		builder.setNegativeButton("No", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int buttonId) {
				listener.onNo();
			}
		});
		
		builder.create().show();
	}
	
	/**
	 * Dialog for select one of possible social account types
	 * @param context current dialog context
	 * @param title dialog header
	 * @param dialogListener dialog listener
	 */
	public static void showSelectAccountTypeDialog(Activity context, String title, OnClickListener dialogListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setAdapter(new AccountsTypesListAdapter(context, AccountTypesProvider.accountTypes), dialogListener);
		builder.create().show();
	}
	
	/**
	 * Show dialog for choosing one of registered in application social account
	 * @param context current context for dialog
	 * @param title dialog header
	 * @param registeredAccounts list of registered accounts (gathered from local db)
	 * @param dialogListener dialog callback
	 */
	public static void showSelectAccountDialog(Activity context, String title, final List<SocialAccount> registeredAccounts,
			final ISelectAccountDialogListener dialogListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setAdapter(new AccountsListAdapter(context, registeredAccounts), new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int selectedIndex) {
				SocialAccount selectedAccount = registeredAccounts.get(selectedIndex);
				dialogListener.accountSelected(selectedAccount);
			}
		});
		builder.create().show();
	}
	
	/**
	 * Setup account dialog. Contains edit fields for 'username' and 'password' fields
	 * @param context current context for dialog
	 * @param accountTypeId type of social account (listed in AccountType class)
	 * @param listener dialog listener
	 */
	public static void showSetupAccountDialog(Activity context, final int accountTypeId, final ISetupAccountDialogListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		LayoutInflater li = LayoutInflater.from(context);
		final View view = li.inflate(R.layout.setup_account_dialog, null);
		builder.setView(view);
		
		builder.setTitle(AccountTypesProvider.accountTypes.get(accountTypeId).title + " account");
		builder.setIcon(AccountTypesProvider.accountTypes.get(accountTypeId).bigIconId);

		builder.setPositiveButton("OK", new OnClickListener() {
			@Override
			public void onClick(DialogInterface di, int buttonId) {
				EditText usernameTextbox = (EditText) view.findViewById(R.id.username_textbox);
				EditText passwordTextbox = (EditText) view.findViewById(R.id.password_textbox);
				String username = usernameTextbox.getText().toString();
				String password = passwordTextbox.getText().toString();
				SocialAccount account = new SocialAccount(username, password, AccountTypesProvider.accountTypes.get(accountTypeId));
				
				listener.dataGathered(account);
			}
		});
		builder.setNegativeButton("Cancel", null);
		
		builder.create().show();
	}
	
	/**
	 * Show toast with icon and text message
	 * @param context current context for toast
	 * @param message toast text message
	 */
	public static void showIconifiedToast(Activity context, String message) {
		LayoutInflater inflater = context.getLayoutInflater();
		View view = inflater.inflate(R.layout.iconified_toast, (ViewGroup) context.findViewById(R.id.toast_root));
		
		TextView toastMessageTextView = (TextView) view.findViewById(R.id.toast_message);
		toastMessageTextView.setText(message);
		
		Toast toast = new Toast(context.getApplicationContext());
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(view);
		toast.show();
	}
	
	/**
	 * Show single toast message
	 * @param context current context for toast
	 * @param message toast text message
	 */
	public static void showToast(Activity context, String message) {
		Toast toast = Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
	
	/**
	 * Display dialog with account's details info
	 * @param context application context for dialog
	 */
	public static void showAccountDetailsDialog(Activity context, final Drawable avatarDrawable, SocialAccount account, int postsCount) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		LayoutInflater li = LayoutInflater.from(context);
		final View view = li.inflate(R.layout.account_details, null);
		builder.setView(view);
		
		// pass data into dialog widgets
		TextView usernameTextView = (TextView) view.findViewById(R.id.username_text);
		usernameTextView.setText(account.username);
		
		TextView accountTypeTitleTextView = (TextView) view.findViewById(R.id.account_type_text);
		accountTypeTitleTextView.setText(account.type.title);
		
		TextView postsCountTextView = (TextView) view.findViewById(R.id.posted_count_text);
		postsCountTextView.setText(String.valueOf(postsCount));
		
		if (avatarDrawable != null) {
			// display account avatar
			ImageView avatarImage = (ImageView) view.findViewById(R.id.avatar_icon);
			avatarImage.setImageDrawable(avatarDrawable);
		}
		
		builder.setTitle("Account details");
		builder.setIcon(account.type.bigIconId);
		
		builder.setNegativeButton("OK", new OnClickListener() {
			@Override public void onClick(DialogInterface dialog, int which) { }
		});
		
		builder.create().show();
	}
}
